#pragma once

#include "SPEmitter.h"

namespace SP
{
	class DLL_EXPORT SPEmitterParticle : public SPEmitter
	{
	public:
		SPEmitterParticle();
		SPEmitterParticle( SPEmitterParticle* archetype );
		virtual ~SPEmitterParticle();

		void Create( SPEmitterParticle* archetype );

		virtual void Initialize();
		virtual void Render();
		virtual void Advance( float fElapsedTime );
		virtual void ClearModule();
		virtual void CreateFileBuffer( SPFileBuffer* );
		virtual void CreateFromFileBuffer( BYTE* );
		virtual bool SetSPModule( SPModule* pModule );

		void					Create( UINT maxParticles );
		void					Create( float fSpawnRate, float fParticleLifeTime );

		UINT					GetInitialAllocationCount(){ return m_nMaxAllocationCount; }

		HRESULT			CreateTextureFromFile( const wchar_t* file );
		void					EnableDepthSort( bool bSort ){ m_bSort = bSort; }

		void					AbsParticlePosition( bool bAbs ){ m_bAbsParticlePosition = bAbs; }
		void					AbsParticleVelocity( bool bAbs ){ m_bAbsParticleVelocity = bAbs; }
		void					AbsParticleAcceleration( bool bAbs ){ m_bAbsParticleAcceleration = bAbs; }

		virtual bool IsDead();

	protected:				
		HRESULT			CreateParticle( UINT nInitialAllocationCount );
		void					ReleaseParticle();

		void					EmitParticle( UINT idxParticle );

		void					UpdateParticleBuffer();

		FLOAT				m_fParticleEmitRemain;
		FLOAT				m_fParticleLifeTime;
		UINT					m_nMaxAllocationCount;
		UINT					m_nDrawCount;
		UINT					m_nNextParticlesIdx;
		FLOAT				m_fSpawnRate;
		bool					m_bProcessSpawnRate;
		
		// Texture
		ID3D11Resource*						m_pParticleTex;
		ID3D11ShaderResourceView*	m_pParticleTexSRV;

		// Life
		FLOAT*										m_pParticleLife;

		// Sort & Index
		bool											m_bSort;
		FLOAT*										m_pParticleDepthArray;
		DWORD*									m_pIndex;
		ID3D11Buffer*							m_pIndexBuffer;
		ID3D11Buffer*							m_pVertexBuffer;

		// Position
		bool					m_bAbsParticlePosition;
		bool					m_bAbsParticleVelocity;
		bool					m_bAbsParticleAcceleration;

		XMFLOAT3*								m_pPosition;		
		ID3D11ShaderResourceView*	m_pPositionSRV;
		ID3D11Buffer*							m_pPositionBuffer;

		// Size
		FLOAT*										m_pSize;
		ID3D11ShaderResourceView*	m_pSizeSRV;
		ID3D11Buffer*							m_pSizeBuffer;

		// Colour
		XMFLOAT4*								m_pColour;
		ID3D11ShaderResourceView*	m_pColourSRV;
		ID3D11Buffer*							m_pColourBuffer;

		// Modules
		SPModuleParticleVector*			m_pModulePosition;
		SPModuleParticleVector*			m_pModuleVelocity;
		SPModuleParticleVector*			m_pModuleAcceleration;
		SPModuleParticleFloat*				m_pModuleSize;
		SPModuleParticleVector*			m_pModuleColour;
		SPModuleParticleFloat*				m_pModuleAlpha;

		void SetZero();

		friend class SPEmitterParticleRenderer;

	};
}


